<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CommonWord;
use App\Constants\MiniProgramConstant;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CommonWordController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CommonWord(), function (Grid $grid) {
            $grid->model()->orderByDesc('id');

            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->column('id');
            $grid->column('mini_type')->display(function ($miniType) use ($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('content');
            $grid->column('status')->using(['否', '是'])->label(['danger', 'primary']);
            $grid->column('sort');

            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableBatchDelete();
            $grid->disableViewButton();
            $grid->disableRowSelector();

            $grid->filter(function (Grid\Filter $filter) use ($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->like('name')->width(2);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CommonWord(), function (Show $show) {
            $show->field('id');
            $show->field('content');
            $show->field('sort');
            $show->field('status');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CommonWord(), function (Form $form) {
            $form->display('id');
            $form->select('mini_type')->options(function () {
                return getAuthMiniProgramMap(Admin::user()->id);
            })->required();
            $form->text('content')->required()->rules();
            $form->number('sort')->default(0);
            $form->radio('status')->options(['否', '是'])->default(0);

            $form->submitted(function (Form $form) {
                $content = $form->content;
                $id = $form->getKey();

                $exists = \App\Models\CommonWords::where('content', '=', $content)
                    ->where('mini_type', '=', $form->mini_type)
                    ->when($id, function ($query, $id) {
                        return $query->where('id', '!=', $id);
                    })->exists();

                if ($exists) {
                    $form->responseValidationMessages('content', '已存在');
                }
            });
        });
    }
}
